package com.onpositive.mediawiki.serializing;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.GregorianCalendar;

import com.onpositive.data.serializer.writers.DateWithAccuracyWriter;
import com.onpositive.data.serializer.writers.RangedDateWriter;
import com.onpositive.data.units.DateWithAccuracy;
import com.onpositive.datamap.values.impl.DateRange;
import com.onpositive.datamap.values.impl.ScalarRange;
import com.onpositive.units.ParsedDate;

public class DateValueWriter extends AbstractMediaWikiWriter<ParsedDate> {
	
	GregorianCalendar cl=new GregorianCalendar();
	RangedDateWriter dw = new RangedDateWriter() ;
	
	public DateValueWriter() {
		super(ParsedDate.class);
	}

	@Override
	protected void append( ParsedDate obj ) {
		
		if( obj == null ){
			dw.append( null ) ;
			return ;
		}
		
		DateWithAccuracy lVal, uVal ;
		String comment = obj.getComment() ;
		
		if( obj.isRange() ){
			 lVal = obj.getLowerBound() ;
			 uVal = obj.getUpperBound() ;
		} else{
			lVal = obj.getUpperBound() ;
			uVal = obj.getUpperBound() ;
		}
		
		DateRange dr = new DateRange( null, comment, lVal, uVal ) ;
		dw.append( dr ) ;
	}

	@Override
	public void serialize(DataOutputStream stream) throws IOException {
		dw.serialize( stream ) ;		
	}
}
